﻿using System;
using System.Globalization;
using Business;
using UI_MasterPage;

namespace UI_Main
{
    public partial class UiMainSearch : System.Web.UI.Page
    {
        private ArticleBusiness _articleBusiness;

        protected void Page_Init(object sender, EventArgs e)
        {
            var masterPage = (PresentationMain) Master;
            if (masterPage != null) _articleBusiness = masterPage.ArticleBusiness;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["keyword"] == null)
            {
                Response.Redirect("Default.aspx");
            }

            string keyword = Request.QueryString["keyword"];

            int page = 1;
            if (Request.QueryString["page"] != null)
            {
                page = Int32.Parse(Request.QueryString["page"].ToString(CultureInfo.InvariantCulture));
            }

            var paginatedInfo = _articleBusiness.GetAll(keyword, page, 2);
            PageNavigator.CurrentPage = paginatedInfo.CurrentPage;
            PageNavigator.VisiblePages = 5;
            PageNavigator.TotalPages = paginatedInfo.TotalPages;
            PageNavigator.NavigatePage = "~/UI_Main/Search.aspx?keyword=" + keyword;

            ArticleListView.DataSource = paginatedInfo.Data;
            ArticleListView.DataBind();
        }
    }
}